<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>系统</title>
<%@include file="/WEB-INF/jsp/include/easyui_1_5_3.jsp"%>

<script type="text/javascript">
	//请求地址
	var url;
	//提示消息
	var mesTitle;

	//页面加载后执行
	$(function(){
		$('#unit_id_form').combotree({
			url: path+'/sysUnitInfo/getBy',
			panelHeight: '120',
			editable: 'true',
// 			valueField:'code',
// 			textField:'name',
// 			keyHandler: function(q, row){//需要扩展才能实现：easyui  树形选择框 根据输入筛选
// 			    var opts = $(this).combotree('options');
// 			    var r = false ;
// 			    if(row['id']){
// 			    	r = row[opts.textField].indexOf(q) >= 0 || 
// 			    		row['id'].indexOf(q) >= 0 || 
// 			    		row['id'].indexOf(q.toUpperCase()) >= 0;
// 			    }else{
// 			    	r = row[opts.textField].indexOf(q) >= 0 || 
// 			    		row['id'].indexOf(q) >= 0 ;
// 			    }
// 			    return r;
// 			},
			onSelect: function(rec){
			}
		});
    });
	//添加用户信息
	function addSysRoles(){
		$('#dlg').dialog('open').dialog('setTitle','新增');
		$('#fm').form('clear');
		url=path+"/sysRoles/add";
		mesTitle = '新增成功';
	}
	
	//编辑用户信息
 	function editSysRoles(){
	 	var row = $('#datagrid').datagrid('getSelected');
	 	if (row){
	 		var role_code = row.role_code;
		 	$('#dlg').dialog('open').dialog('setTitle','编辑');
		 	$('#fm').form('load',row);
		 	url = path+"/sysRoles/modify";
		 	mesTitle = '编辑成功';
	 	}else{
	 		$.messager.alert('提示', '请选择要编辑的记录！', 'error');
	 	}
	}
 	
	//删除用信息
 	function deleteSysRoles(){
	 	var row = $('#datagrid').datagrid('getSelected');
	 	if (row){
	 		var role_code = row.role_code;
		 	$('#dlg_delete').dialog('open').dialog('setTitle','删除');
		 	$('#fmdelete').form('load',row);
		 	url = path+"/sysRoles/delete?role_code="+role_code;
		 	mesTitle = '删除成功';
	 	}else{
	 		$.messager.alert('提示', '请选择要删除的记录！', 'error');
	 	}
	}
 	
	//保存添加、修改内容
	function saveSysRoles(){
	 	$('#fm').form('submit',{
		 	url: url,
		 	onSubmit: function(){
		 		return $(this).form('validate');
		 	},
			success: function(result){
				/* console.info(result); */
				var result = eval('('+result+')');
				if (result.success){
				 	$('#dlg').dialog('close'); 
				 	$('#datagrid').datagrid('reload'); 
				} else {
					 mesTitle = '新增失败';
				}
				$.messager.show({ 
					 title: mesTitle,
					 msg: result.msg
				});
			}
	 	});
	}	
	
	//提交删除内容
	function saveSysRoles_del(){
	 	$('#fmdelete').form('submit',{
		 	url: url,
		 	onSubmit: function(){
		 		return $(this).form('validate');
		 	},
			success: function(result){
				/* console.info(result); */
				var result = eval('('+result+')');
				if (result.success){
				 	$('#dlg_delete').dialog('close'); 
				 	$('#datagrid').datagrid('reload'); 
				} else {
					 mesTitle = '新增失败';
				}
				$.messager.show({ 
					 title: mesTitle,
					 msg: result.msg
				});
			}
	 	});
	}

	//快速查询
 	function searchSysRolesQ(){
	 	$("#datagrid").datagrid("load", {
            "username": $('#search_username').val()
        });
	}
	
	//查询
 	function searchSysRoles(){
		$('#dlgsearch').dialog('open').dialog('setTitle','查询');
		$('#fmsearch').form('clear');
		url = path+"/user/searchUser";
		mesTitle = '查询成功';
	}
	
	//刷新
	function reload(){
		$('#datagrid').datagrid('reload'); 
	}

	//编辑权限
	function editRole(id){
	 	var row = $('#datagrid').datagrid('getSelected');
	 	if (!row){
	 		$.messager.alert('提示', '请选择要编辑的记录！', 'error');
	 	}
	 	var role_code = row.role_code;
	    //1.取消所有选择
	    //var root = $('#quanxian_tree').tree('getRoot');  
	    //$("#quanxian_tree").tree('uncheck',root.target);  
	    //2.加载权限，动态选择
	    var url = path+"/sysMenu/getMenuByRole?role_code="+role_code;
	    $.ajax({
	        cache : true,
	        type : "POST",
	        url : url,
	        async : false,
	        success : function(data) {
// 	        	alert(data);
	            data = JSON.parse(data);	//如果是fastjson返回则需要
// 	             $(data).each(function(i, obj){
	             $.each(data,function(i, obj){//如果是fastjson返回则需要
	            	 checkTreeNode(obj);
	             });
	        }
	    });
	    
	    $("#quanx_win").window('open');
	}
	function checkTreeNode(obj){
        var children = obj.children;
        if(children==''||children==null){
//          $(obj).each(function(j,c_obj){
            var cc = $("#quanxian_tree").tree('find',obj.id);
            if(cc){
                $("#quanxian_tree").tree('check',cc.target);
            }
//         });
        }else{
            $(children).each(function(j,c_obj){
        		checkTreeNode(c_obj);
            });
        }
	}


	//获取选中节点和父节点
	function getChecked(){
	    var nodesParent = $('#quanxian_tree').tree('getChecked','indeterminate');
	    var nodes = $('#quanxian_tree').tree('getChecked');
	    var s = '';
	    
	    for (var i = 0; i < nodesParent.length; i++) {
	        if (s != '') 
	            s += ',';
	        s += nodesParent[i].id;
	    }
	    for (var i = 0; i < nodes.length; i++) {
	        if (s != '') 
	            s += ',';
	        s += nodes[i].id;
	    }
	   return s;
	}


	//提交修改
	function confrimEdit(){
	 	var row = $('#datagrid').datagrid('getSelected');
	 	if (!row){
	 		$.messager.alert('提示', '请选择要编辑的记录！', 'error');
	 	}
	 	var role_code = row.role_code;
	    var menuIds = getChecked();
	    var url = path+"/sysRolesMenu/addForRole";
	    var data = {
	            "role_code" :role_code,
	            "menuIds":menuIds
	        };
	    $.ajax({
	        cache : true,
	        type : "POST",
	        url : url,
	        data: data,
	        async : true,
	        success : function(result) {
// 	        	alert(result);
// 				var result = eval('('+result+')');
// 				alert(result.success)
				if (result.success){
	                closeWin();
	                $.messager.show({
	                    msg : result.msg,
	                    title : '成功'
	                });
				} else {
					 mesTitle = '新增失败';
		             $.messager.alert(
		                        '错误',
		                        result.msg,
		                        'error');
				}
	        }
	    });
	}

	//关闭窗口
	function closeWin(){
	     $('#quanxian_tree').tree('reload'); 
	     $("#quanx_win").window('close');
	}
	
</script>

</head>
<body class="easyui-layout" fit="true">
	<div region="center" border="false" style="overflow: hidden;">
		<!-- 信息列表 title="管理" -->
		<table id="datagrid" class="easyui-datagrid" 
		    fit="true"
			url="${path}/sysRoles/datagrid" 
			toolbar="#toolbar" 
			pagination="true"
			fitColumns="false" 
			singleSelect="false" 
			rownumbers="true"
			striped="true"
			border="false" 
			nowrap="false">
			<thead>
				<tr>
					<th field="unitName" width="100">单位</th>
					<th field="role_code" width="100">权限编码</th>
					<th field="role_name" width="100">权限名称</th>
					<th field="num" width="100">权限排序</th>
					<th field="role_description" width="100">权限描述</th>
					<th field="unit_id" width="100" >单位ID</th>

				</tr>
			</thead>
		</table>

		<!-- 按钮 -->
		<div id="toolbar">
			<a href="javascript:void(0);" class="easyui-linkbutton"
				iconCls="icon-reload" plain="true" onclick="reload();">刷新</a>
			<a href="javascript:void(0);" class="easyui-linkbutton"
				iconCls="icon-add" plain="true" onclick="addSysRoles();">新增</a> 
			<a href="javascript:void(0);" class="easyui-linkbutton"
				iconCls="icon-edit" plain="true" onclick="editSysRoles();">编辑</a> 
			<a href="javascript:void(0);" class="easyui-linkbutton"
				iconCls="icon-remove" plain="true" onclick="deleteSysRoles();">删除</a>
			<a href="javascript:void(0);" class="easyui-linkbutton"
				iconCls="icon-edit" plain="true" onclick="editRole();">分配权限</a> 
			<span>用户名:</span><input name="search_username" id="search_username" value="" size=10 /> 
  			<a href="javascript:searchSysRolesQ()" class="easyui-linkbutton" data-options="iconCls:'icon-search'">查询</a> 
			<a href="javascript:void(0);" class="easyui-linkbutton"
				iconCls="icon-jright" plain="true" onclick="searchSysRoles();">更多查询</a>
		</div>

		<!-- 添加/修改对话框 -->
		<div id="dlg" class="easyui-dialog"
			style="width:600px;height:400px;padding:30px 20px" closed="true"
			buttons="#dlg_AM-buttons">
			<form id="fm" method="post" novalidate>
				<table>
					<tr>
						<td>
							<label>单位ID:</label>
						</td>
						<td>
<!-- 							<input name="unit_id" class="easyui-validatebox" required="true"> -->
							<select id="unit_id_form" name="unit_id" class="easyui-combotree" required="true" />
						</td>
					</tr>
					<tr>
						<td>
							<label>权限编码:</label>
						</td>
						<td>
							<input name="role_code" class="easyui-validatebox" required="true">
						</td>
					</tr>
					<tr>
						<td>
							<label>权限名称:</label>
						</td>
						<td>
							<input name="role_name" class="easyui-validatebox" required="true">
						</td>
					</tr>
					<tr>
						<td>
							<label>权限排序:</label>
						</td>
						<td>
							<input name="num" class="easyui-validatebox" required="true">
						</td>
					</tr>
					<tr>
						<td>
							<label>权限描述:</label>
						</td>
						<td>
							<input name="role_description" class="easyui-validatebox" required="true">
						</td>
					</tr>

				</table>
			</form>
		</div>
		
		<!-- 添加/修改对话框按钮 -->
		<div id="dlg_AM-buttons">
			<a href="javascript:void(0)" class="easyui-linkbutton c6"
				iconCls="icon-ok" onclick="saveSysRoles()" style="width:90px">保存</a> 
				<a href="javascript:void(0)" class="easyui-linkbutton"
				iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')"
				style="width:90px">取消</a>
		</div>
		
		<!-- 删除对话框 -->
		<div id="dlg_delete" class="easyui-dialog"
			style="width:300px;height:200px;padding:30px 20px" closed="true"
			buttons="#dlg-del-buttons">
			<div class="ftitle">请谨慎操作</div>
			<form id="fmdelete" method="post" novalidate>
					<label>确定删除吗？</label>
			</form>
		</div>
		
		<!-- 删除对话框按钮 -->
		<div id="dlg-del-buttons">
			<a href="javascript:void(0)" class="easyui-linkbutton c6"
				iconCls="icon-ok" onclick="saveSysRoles_del()" style="width:90px">删除</a> 
			<a href="javascript:void(0)" class="easyui-linkbutton"
				iconCls="icon-cancel" onclick="javascript:$('#dlg_delete').dialog('close')"
				style="width:90px">取消</a>
		</div>
		
		<!-- 查询对话框 -->
		<div id="dlgsearch" class="easyui-dialog"
			style="width:400px;height:380px;padding:30px 20px" closed="true"
			buttons="#dlg_query-buttons">
			<form id="fmsearch" method="post" novalidate>
				<label>权限:</label>  
                    <span id="span1" style="display: inline-block;"></span>  
                </div>
			</form>
		</div>
		
		<!-- 查询对话框按钮 -->
		<div id="dlg_query-buttons">
			<a href="javascript:void(0)" class="easyui-linkbutton c6"
				iconCls="icon-ok" onclick="searchSysRoles()" style="width:90px">查询</a> 
				<a href="javascript:void(0)" class="easyui-linkbutton"
				iconCls="icon-cancel" onclick="javascript:$('#dlgsearch').dialog('close')"
				style="width:90px">取消</a>
		</div>
		<!-- 权限编辑窗口 -->
		<div id="quanx_win" class="easyui-window" title="权限编辑"
		    style="width: 500px; height: 250px;" closed="true">
		    <form style="padding: 10px 20px 10px 40px;">
		        <ul id="quanxian_tree" class="easyui-tree" url="<%=path %>/getMenu" method="post"
		            checkbox="true">
		        </ul>
		        <div style="padding: 5px; text-align: center;">
		            <a href="#" class="easyui-linkbutton" icon="icon-ok" onclick="confrimEdit();">确定</a>
		            <a href="#" class="easyui-linkbutton" icon="icon-cancel" onclick="closeWin();">取消</a>
		        </div>
		    </form>
		</div>
	</div>
</body>
</html>
